import rules from './rules'
export default {
  install (Vue) {
    Vue.prototype._vaildationMap = new Map()
    Vue.prototype.$vaildation = function (vm) {
      let items = [...this._vaildationMap.values()]
      let status = []
      return new Promise((resolve, reject) => {
        for (let x of items) {
          let t = vm[x.query]
          if (Object.prototype.toString.call(t) === '[object String]') {
            t = t.replace(/^\s+|\s+$/g, '')
          }
          if (x.retrieve) {
            status.push(rules.retrieve(x, vm))
          } else if (x.area) {
            status.push(rules.area(x, vm))
          } else if (t) {
            if (!vm.hasOwnProperty(x.query)) {
              status.push(true)
              continue
            }
            if (x.min) {
              status.push(rules.min(x, vm))
            }
            if (x.max) {
              status.push(rules.max(x, vm))
            }
            if (x.phone) {
              status.push(rules.phone(x, vm))
            }
            if (x.email) {
              status.push(rules.email(x, vm))
            }
            if (x.noPhone) {
              status.push(rules.noPhone(x, vm))
            }
            if (x.tel) {
              status.push(rules.tel(x, vm))
            }
            if (x.special) {
              status.push(rules.special(x, vm))
            }
            if (x.number) {
              status.push(rules.number(x, vm))
            }
            if (x.numberDot) {
              status.push(rules.numberDot(x, vm))
            }
            if (x.maxNum) {
              status.push(rules.maxNum(x, vm))
            }
            if (x.minNum) {
              status.push(rules.minNum(x, vm))
            }
            if (x.password) {
              status.push(rules.password(x, vm))
            }
            if (x.ip) {
              status.push(rules.ip(x, vm))
            }
            if (x.date) {
              status.push(rules.date(x, vm))
            }
          } else {
            if (!vm.hasOwnProperty(x.query)) {
              status.push(true)
              continue
            }
            if (x.required) {
              status.push(rules.required(x, vm))
            }
            if (x.select) {
              status.push(rules.select(x, vm))
            }
          }
        }
        if (status.every((x) => x)) {
          resolve()
        } else {
          reject()
        }
      })
    }
    Vue.prototype.$input = function (vm) {
      let {type, vnode} = vm
      switch (type) {
        case 'money':
          rules.money(vnode.elm)
          break
        case 'moneywithminus':
          rules.moneywithminus(vnode.elm)
          break
        case 'day':
          rules.day(vnode.elm)
          break
      }
    }
  }
}
